package com.sparrow.source.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sparrow.source.common.dto.Result;
import com.sparrow.source.common.dto.UnvDeviceQueryRequest;
import com.sparrow.source.common.enums.ResultEnum;
import com.sparrow.source.persistense.UnvDevice;
import com.sparrow.source.service.IUnvDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 设备管理
 *
 * @author bryceyung
 * @since 2024/11/19
 */
@Controller
@RequestMapping("/v1/device")
public class DeviceController {

    @Autowired
    private IUnvDeviceService unvDeviceService;

    @PostMapping("add")
    @ResponseBody
    public Result addDevice(@RequestBody UnvDevice device) {
        device.setCreateTime(new Date());
        return Result.build(ResultEnum.SUCCESS).setObject(unvDeviceService.addDevice(device));
    }

    /**
     * 设备信息同步
     *
     * @param deviceId
     * @return
     */
    @PostMapping("sync")
    @ResponseBody
    public Result syncDeviceInfo(@RequestParam("deviceId") Integer deviceId) {
        UnvDevice device = unvDeviceService.sync(deviceId);
        if (device == null) {
            return Result.build(ResultEnum.NO_CONTENT).setMessage("设备信息获取失败");
        }
        return Result.build(ResultEnum.SUCCESS).setObject(unvDeviceService.getById(deviceId));
    }

    /**
     * 设备信息分页查询
     *
     * @param page
     * @param queryRequest
     * @return
     */
    @PostMapping("/query/page")
    @ResponseBody
    public Result queryDevicePage(Page<UnvDevice> page, @RequestBody UnvDeviceQueryRequest queryRequest) {
        return Result.build(ResultEnum.SUCCESS).setObject(unvDeviceService.queryDevicePage(queryRequest, page));
    }

    /**
     * 下发人脸信息
     *
     * @param deviceId
     * @param uids
     * @return
     */
    @PostMapping("syncUserInfoToDevice")
    @ResponseBody
    public Result syncUserInfoToDevice(@RequestParam("deviceId") Integer deviceId, @RequestParam List<Integer> uids) {
        return unvDeviceService.addPersonInfo(deviceId, uids);
    }

    /**
     * 向设备同步所有人员
     *
     * @param deviceId
     * @return
     */
    @PostMapping("syncAllUserInfo")
    @ResponseBody
    public Result syncAllUserInfo(@RequestParam("deviceId") Integer deviceId) {
        return unvDeviceService.syncAllPersonInfo(deviceId);
    }

    /**
     * 更新设备信息
     *
     * @param device
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public Result updateDevice(@RequestBody UnvDevice device) {
        return Result.build(ResultEnum.SUCCESS).setObject(unvDeviceService.updateById(device));
    }

    /**
     * 查询设备详情信息
     *
     * @param id
     * @return
     */
    @PostMapping("/getInfo")
    @ResponseBody
    public Result queryDetailById(@RequestParam("id") Integer id) {
        return Result.build(ResultEnum.SUCCESS).setObject(unvDeviceService.getById(id));
    }

    /**
     * 查询设备绑定的人员列表
     *
     * @param deviceId
     * @return
     */
    @PostMapping("queryDeviceUser")
    @ResponseBody
    public Result queryDeviceUsers(@RequestParam("deviceId") Integer deviceId) {
        return Result.build(ResultEnum.SUCCESS).setObject(unvDeviceService.queryDeviceUserList(deviceId));
    }
}
